﻿using AudioBooks.Data;
using SQLite;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;

namespace AudioBooks.SQLite
{
    public class HandleBookmark
    {
        public static bool DeleteBookMark(int bookMarkId)
        {
            using (var db = new SQLiteConnection(App.DatabasePath))
            {
                var dataUser = (db.Table<PlayChapterInfo>()).ToList();
                if (dataUser != null && dataUser.Count > 0)
                {
                    var delete=dataUser.FirstOrDefault(s=>s.KeyMark==bookMarkId);
                    if(delete!=null)
                    {
                        db.Delete<PlayChapterInfo>(bookMarkId);
                        return true;
                    }
                }
            }
            return false;
        }
        public static void DeleteAllBookMark(string bookId)
        {
            using (var db = new SQLiteConnection(App.DatabasePath))
            {
                var dataUser = (db.Table<PlayChapterInfo>()).ToList();
                if (dataUser != null && dataUser.Count > 0)
                {
                    foreach (var playChapterInfo in dataUser)
                    {
                        if (playChapterInfo.BookId == bookId)
                            db.Delete<PlayChapterInfo>(playChapterInfo.KeyMark);
                    } 
                }
            }
           
        }
        public static async Task<bool> DeleteBook(string bookId)
        {
            using (var db = new SQLiteConnection(App.DatabasePath))
            {
                var dataUser = (db.Table<FileDownLoadComplete>()).ToList();
                if (dataUser != null && dataUser.Count > 0)
                {
                    var delete = dataUser.Where(s => s.BookId == bookId).ToList();
                    if (delete.Count>0)
                    {
                        foreach(var d in delete)
                        {
                            db.Delete<FileDownLoadComplete>(d.KeyBooks);
                        }
                        var getFolderInfo = await ApplicationData.Current.LocalFolder.TryGetItemAsync(bookId);
                        if (getFolderInfo != null)
                            await getFolderInfo.DeleteAsync(StorageDeleteOption.Default);
                        return true;
                    }
                }
            }
            return false;
        }
    }
}
